package com.bolang.onlycode.demo.word;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;

import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FontRegistry {
    private static Map<String, BaseFont> fontMap = new HashMap<>();
    static {
        fontMap.put("SIMSUN", register("src/main/resources/simsun.ttc,0"));
    }

    private static BaseFont register(String fontPath) {
        try {
            return BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static IFontProvider get(String familyName) {
        return (familyName0, encoding, size, style, color) -> {
            BaseFont baseFont = fontMap.get(familyName);
            if (baseFont == null) {
                return new Font();
            }
            return new Font(baseFont, 10.5f, Font.NORMAL, Color.BLACK);
        };
    }

}
